home *** CD-ROM | disk | FTP | other *** search
- //========================================================================
- //
- // Object.h
- //
- // Copyright 1996 Derek B. Noonburg
- //
- //========================================================================
-
- #ifndef OBJECT_H
- #define OBJECT_H
-
- #ifdef __GNUC__
- //#pragma interface
- #endif
-
- #include <stdio.h>
- #include <string.h>
- #include "gtypes.h"
- #include "gmem.h"
- #include "GString.h"
- #include "XRef.h"
-
- class Array;
- class Dict;
- class Stream;
-
- //------------------------------------------------------------------------
- // Ref
- //------------------------------------------------------------------------
-
- struct Ref {
- int num; // object number
- int gen; // generation number
- };
-
- //------------------------------------------------------------------------
- // object types
- //------------------------------------------------------------------------
-
- typedef int ObjType;
-
- // simple objects
- #define objBool 0 // boolean
- #define objInt 1 // integer
- #define objReal 2 // real
- #define objString 3 // string
- #define objName 4 // name
- #define objNull 5 // null
-
- // complex objects
- #define objArray 6 // array
- #define objDict 7 // dictionary
- #define objStream 8 // stream
- #define objRef 9 // indirect reference
-
- // special objects
- #define objCmd 10 // command name
- #define objError 11 // error return from Lexer
- #define objEOF 12 // end of file return from Lexer
- #define objNone 13 // uninitialized object
-
- // misc
- #define numObjTypes 14 // number of object types
-
- //------------------------------------------------------------------------
- // Object
- //------------------------------------------------------------------------
-
- class Object {
- public:
-
- // Default constructor.
- Object():
- type(objNone) {}
-
- // Initialize an object.
- Object *initBool(GBool booln1)
- { type = objBool; booln = booln1;
- #ifdef DEBUG_MEM
- ++numAlloc[objBool];
- #endif
- return this; }
- Object *initInt(int intg1)
- { type = objInt; intg = intg1;
- #ifdef DEBUG_MEM
- ++numAlloc[objInt];
- #endif
- return this; }
- Object *initReal(double real1)
- { type = objReal; real = real1;
- #ifdef DEBUG_MEM
- ++numAlloc[objReal];
- #endif
- return this; }
- Object *initString(char *string1, int length)
- { type = objString; string = new GString(string1, length);
- #ifdef DEBUG_MEM
- ++numAlloc[objString];
- #endif
- return this; }
- Object *initName(char *name1)
- { type = objName; name = copyString(name1);
- #ifdef DEBUG_MEM
- ++numAlloc[objName];
- #endif
- return this; }
- Object *initNull()
- { type = objNull;
- #ifdef DEBUG_MEM
- ++numAlloc[objNull];
- #endif
- return this; }
- Object *initArray();
- Object *initDict();
- Object *initStream(Stream *stream1);
- Object *initRef(int num1, int gen1)
- { type = objRef; ref.num = num1; ref.gen = gen1;
- #ifdef DEBUG_MEM
- ++numAlloc[objRef];
- #endif
- return this; }
- Object *initCmd(char *cmd1)
- { type = objCmd; cmd = copyString(cmd1);
- #ifdef DEBUG_MEM
- ++numAlloc[objCmd];
- #endif
- return this; }
- Object *initError()
- { type = objError;
- #ifdef DEBUG_MEM
- ++numAlloc[objError];
- #endif
- return this; }
- Object *initEOF()
- { type = objEOF;
- #ifdef DEBUG_MEM
- ++numAlloc[objEOF];
- #endif
- return this; }
-
- // Copy an object.
- Object *copy(Object *obj);
-
- // If object is a Ref, fetch and return the referenced object.
- // Otherwise, return a copy of the object.
- Object *fetch(Object *obj)
- { return (type == objRef && xref) ?
- xref->fetch(ref.num, ref.gen, obj) : copy(obj); }
-
- // Free object contents.
- void free();
-
- // Type checking.
- GBool isBool() { return type == objBool; }
- GBool isInt() { return type == objInt; }
- GBool isReal() { return type == objReal; }
- GBool isNum() { return type == objInt || type == objReal; }
- GBool isString() { return type == objString; }
- GBool isName() { return type == objName; }
- GBool isNull() { return type == objNull; }
- GBool isArray() { return type == objArray; }
- GBool isDict() { return type == objDict; }
- GBool isStream() { return type == objStream; }
- GBool isRef() { return type == objRef; }
- GBool isCmd() { return type == objCmd; }
- GBool isError() { return type == objError; }
- GBool isEOF() { return type == objEOF; }
- GBool isNone() { return type == objNone; }
-
- // Special type checking.
- GBool isName(char *name1)
- { return type == objName && !strcmp(name, name1); }
- GBool isDict(char *dictType);
- GBool isStream(char *dictType);
- GBool isCmd(char *cmd1)
- { return type == objCmd && !strcmp(cmd, cmd1); }
-
- // Accessors. NB: these assume object is of correct type.
- GBool getBool() { return booln; }
- int getInt() { return intg; }
- double getReal() { return real; }
- double getNum() { return type == objInt ? (double)intg : real; }
- GString *getString() { return string; }
- char *getName() { return name; }
- Array *getArray() { return array; }
- Dict *getDict() { return dict; }
- Stream *getStream() { return stream; }
- int getRefNum() { return ref.num; }
- int getRefGen() { return ref.gen; }
-
- // Array accessors.
- int arrayGetLength();
- void arrayAdd(Object *elem);
- Object *arrayGet(int i, Object *obj);
- Object *arrayGetNF(int i, Object *obj);
-
- // Dict accessors.
- int dictGetLength();
- void dictAdd(char *key, Object *val);
- GBool dictIs(char *dictType);
- Object *dictLookup(char *key, Object *obj);
- Object *dictLookupNF(char *key, Object *obj);
- char *dictGetKey(int i);
- Object *dictGetVal(int i, Object *obj);
- Object *dictGetValNF(int i, Object *obj);
-
- // Stream accessors.
- GBool streamIs(char *dictType);
- void streamReset();
- int streamGetChar();
- int streamGetPos();
- void streamSetPos(int pos);
- FILE *streamGetFile();
- Dict *streamGetDict();
-
- // Output.
- char *getTypeName();
- void print(FILE *f = stdout);
-
- // Memory testing.
- static void memCheck(FILE *f);
-
- private:
-
- ObjType type; // object type
- union { // value for each type:
- GBool booln; // boolean
- int intg; // integer
- double real; // real
- GString *string; // string
- char *name; // name
- Array *array; // array
- Dict *dict; // dictionary
- Stream *stream; // stream
- Ref ref; // indirect reference
- char *cmd; // command
- };
-
- #ifdef DEBUG_MEM
- static int // number of each type of object
- numAlloc[numObjTypes]; // currently allocated
- #endif
- };
-
- //------------------------------------------------------------------------
- // Array accessors.
- //------------------------------------------------------------------------
-
- #include "Array.h"
-
- inline int Object::arrayGetLength()
- { return array->getLength(); }
-
- inline void Object::arrayAdd(Object *elem)
- { array->add(elem); }
-
- inline Object *Object::arrayGet(int i, Object *obj)
- { return array->get(i, obj); }
-
- inline Object *Object::arrayGetNF(int i, Object *obj)
- { return array->getNF(i, obj); }
-
- //------------------------------------------------------------------------
- // Dict accessors.
- //------------------------------------------------------------------------
-
- #include "Dict.h"
-
- inline int Object::dictGetLength()
- { return dict->getLength(); }
-
- inline void Object::dictAdd(char *key, Object *val)
- { dict->add(key, val); }
-
- inline GBool Object::dictIs(char *dictType)
- { return dict->is(dictType); }
-
- inline GBool Object::isDict(char *dictType)
- { return type == objDict && dictIs(dictType); }
-
- inline Object *Object::dictLookup(char *key, Object *obj)
- { return dict->lookup(key, obj); }
-
- inline Object *Object::dictLookupNF(char *key, Object *obj)
- { return dict->lookupNF(key, obj); }
-
- inline char *Object::dictGetKey(int i)
- { return dict->getKey(i); }
-
- inline Object *Object::dictGetVal(int i, Object *obj)
- { return dict->getVal(i, obj); }
-
- inline Object *Object::dictGetValNF(int i, Object *obj)
- { return dict->getValNF(i, obj); }
-
- //------------------------------------------------------------------------
- // Stream accessors.
- //------------------------------------------------------------------------
-
- #include "Stream.h"
-
- inline GBool Object::streamIs(char *dictType)
- { return stream->getDict()->is(dictType); }
-
- inline GBool Object::isStream(char *dictType)
- { return type == objStream && streamIs(dictType); }
-
- inline void Object::streamReset()
- { stream->reset(); }
-
- inline int Object::streamGetChar()
- { return stream->getChar(); }
-
- inline int Object::streamGetPos()
- { return stream->getPos(); }
-
- inline void Object::streamSetPos(int pos)
- { stream->setPos(pos); }
-
- inline FILE *Object::streamGetFile()
- { return stream->getFile(); }
-
- inline Dict *Object::streamGetDict()
- { return stream->getDict(); }
-
- #endif
-